package com.feihong.yw189.bluetooth.utils.djlm.DJLMReader3.reader;

import android.bluetooth.BluetoothDevice;
import android.util.Log;

import java.lang.reflect.Method;


/**
 * Created by Sabirjan on 2017/12/4.
 * DJLMReader.
 * 飞宏电子
 */

public class BleDeviceTools {

    /***
     * createBond 设备配对
     * @param paramBluetoothDevice
     * @return
     */
    public boolean createBond(BluetoothDevice paramBluetoothDevice) {

        if(paramBluetoothDevice==null)
        {
            Log.i("蓝牙", "paramBluetoothDevice is NULL");
            return false;
        }
        if (paramBluetoothDevice.getBondState() != 12) {
            int retCount  =  3;
            int bondState  =   paramBluetoothDevice.getBondState();
            while (retCount>0&&bondState!=12)
            {
                try {
                    Log.i("蓝牙", "connectBt: "+"没有配对");
                    Method localMethod = BluetoothDevice.class.getMethod("createBond", new Class[0]);
                    localMethod.invoke(paramBluetoothDevice, new Object[0]);
                } catch (Exception localException1) {
                    localException1.printStackTrace();
                    Log.i("蓝牙", "createBondException: "+localException1.getMessage());
                    //return false;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                bondState  =   paramBluetoothDevice.getBondState();
                retCount--;
            }
        }

        if (paramBluetoothDevice.getBondState() != 12) {
            Log.i("蓝牙", "connectBt: 配对没有成功......");
            return false;
        }
        Log.i("蓝牙", "connectBt: 配对成功......");
        return true;
    }
}
